package net.tensor.tensor.oauth.server.entity;

import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
 * 客户端表
 * @author Q
 */
@Document(collation = "client_details")
public class Client implements ClientDetails, Serializable {
    private static final long serialVersionUID = 2L;

    @Field(name = "id")
    private String id;

    @Field(name = "name")
    private String name="";

    @Field(name = "secret")
    private String secret="";

    @Field(name = "is_secret_required")
    private boolean isSecretRequired=false;

    @Field(name = "authorized_grant_types")
    private Set<String> authorizedGrantTypes=Collections.emptySet();

    @Field(name = "registered_redirect_uri")
    private Set<String> registeredRedirectUri=Collections.emptySet();

    @Field(name = "access_token_validity_seconds")
    private int accessTokenValiditySeconds=1000;

    @Field(name = "refresh_token_validity_seconds")
    private int refreshTokenValiditySeconds=1000;

    @Field(name = "is_auto_approve")
    private Boolean isAutoApprove=false;

    @Field(name = "is_scoped")
    private Boolean isScoped=true;

    @Field(name = "additional_information")
    private Map<String, Object> additionalInformation=Collections.emptyMap();


    @Field(name = "resourceIds")
    private Set<String> resourceIds=Collections.emptySet();
    @Field(name = "scopes")
    private Set<String> scopes=Collections.emptySet();
    @Field(name = "authorities")
    private Collection<GrantedAuthority> authorities= Collections.EMPTY_LIST;


    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public void setClientId(String clientId) {
        this.id = clientId;
    }

    public void setResourceIds(Set<String> resourceIds) {
        this.resourceIds = resourceIds;
    }

    public Set<String> getScopes() {
        return scopes;
    }

    public void setScopes(Set<String> scopes) {
        this.scopes = scopes;
    }

    public void setAuthorities(Collection<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public void setSecretRequired(boolean secretRequired) {
        isSecretRequired = secretRequired;
    }

    public void setAuthorizedGrantTypes(Set<String> authorizedGrantTypes) {
        this.authorizedGrantTypes = authorizedGrantTypes;
    }

    public void setRegisteredRedirectUri(Set<String> registeredRedirectUri) {
        this.registeredRedirectUri = registeredRedirectUri;
    }

    public void setAccessTokenValiditySeconds(int accessTokenValiditySeconds) {
        this.accessTokenValiditySeconds = accessTokenValiditySeconds;
    }

    public void setRefreshTokenValiditySeconds(int refreshTokenValiditySeconds) {
        this.refreshTokenValiditySeconds = refreshTokenValiditySeconds;
    }

    public Boolean getAutoApprove() {
        return isAutoApprove;
    }

    public void setAutoApprove(Boolean autoApprove) {
        isAutoApprove = autoApprove;
    }

    public Boolean getScoped() {
        return isScoped;
    }

    public void setScoped(Boolean scoped) {
        isScoped = scoped;
    }

    public void setAdditionalInformation(Map<String, Object> additionalInformation) {
        this.additionalInformation = additionalInformation;
    }

    @Override
    public String toString() {
        return "TensorClientDetails{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", secret='" + secret + '\'' +
                ", isSecretRequired=" + isSecretRequired +
                ", authorizedGrantTypes=" + authorizedGrantTypes +
                ", registeredRedirectUri=" + registeredRedirectUri +
                ", accessTokenValiditySeconds=" + accessTokenValiditySeconds +
                ", refreshTokenValiditySeconds=" + refreshTokenValiditySeconds +
                ", isAutoApprove=" + isAutoApprove +
                ", isScoped=" + isScoped +
                ", additionalInformation=" + additionalInformation +
                '}';
    }

    //    ===============    ===========================

    @Override
    public String getClientId() {
        return id;
    }

    @Override
    public boolean isSecretRequired() {
        return isSecretRequired;
    }

    @Override
    public String getClientSecret() {
        return secret;
    }

    @Override
    public boolean isScoped() {
        return isScoped;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return authorizedGrantTypes;
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return registeredRedirectUri;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return accessTokenValiditySeconds;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return refreshTokenValiditySeconds;
    }

    @Override
    public boolean isAutoApprove(String s) {
        return isAutoApprove;
    }

    //    =======================================

    @Override
    public Set<String> getResourceIds() {
        return resourceIds;
    }

    @Override
    public Set<String> getScope() {
        return scopes;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return additionalInformation;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }
}
